<template>
  <div class="app-container">

    <el-tabs :tab-position="tabPosition" @tab-click="tabClick" v-model="activeName">
      <el-tab-pane label="信息发布" name="state1">
        <div class="ouner" v-if="ifShow">
          <div class="head">
            <div>发布列表</div>
            <!-- <div>
              <el-button type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['active:activity:add']"
                >新增</el-button>
            </div> -->
          </div>

          <div class="search">
             <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
              <el-form-item label="标题" prop="title">
                <el-input
                  v-model="queryParams.title"
                  placeholder="请输入标题"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="发布人id " prop="cfShopId">
                <el-input
                  v-model="queryParams.cfShopId"
                  placeholder="请输入发布id "
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <div>
                <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
              </div>

            </el-form>

          </div>


          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          <el-table v-loading="loading" :data="activityList">

            <el-table-column label="标题" align="center" prop="title" />
            <el-table-column label="发布内容" align="center" prop="content" />
            <el-table-column label="发布人 " align="center" prop="principalName" />
            <el-table-column label="上架状态" align="center" prop="state">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.cf_active_state" :value="scope.row.state"/>
              </template>
            </el-table-column>
            <el-table-column label="发布时间" align="center" prop="createTime" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  v-if="scope.row.state==0"
                  size="small"
                  type="primary"
                  @click="handleUpdateState(scope.row)"
                  v-hasPermi="['active:activity:edit']"
                >上架</el-button>
                <el-button
                  v-if="scope.row.state==1"
                  size="small"
                  type="primary"
                  @click="handleUpdateState(scope.row)"
                  v-hasPermi="['active:activity:edit']"
                >下架</el-button>
                <el-button
                  size="small"
                  type="primary"
                  @click="xq(scope.row)"
                >详情</el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
        <div v-else>
          <div style="display: flex; justify-content: space-between; padding: 10px;">
            <div>信息详情</div>
            <div><el-button type="primary" @click="back">返回</el-button></div>
          </div>
          <div style="border-top: 2px solid #E0E0E0; width: 100%;">
            <div class="activity">
              <div class="hang">
                <div class="left">标题</div>
                <div class="right">{{ form.title }}</div>
              </div>
              <div class="hang">
                <div class="left">发布人</div>
                <div class="right">{{ form.principalName }}</div>
              </div>
              <div class="hang">
                <div class="left">发布时间</div>
                <div class="right">{{ form.createTime }}</div>
              </div>
              <div class="hang">
                <div class="left">内容</div>
                <div class="right">{{ form.content }}</div>
              </div>
              <div class="hang">
                <div class="left2">图片</div>
                <div class="right2">{{ form.title }}</div>
              </div>
            </div>
          </div>




        </div>
      </el-tab-pane>
      <el-tab-pane label="待审核"  name="state0" >
        <div class="ouner" v-if="ifShow">
          <div class="head">
            <div>待审核</div>
            <div>
              <el-button type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['active:activity:add']"
                >新增</el-button>
            </div>
          </div>

          <div class="search">
             <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
              <el-form-item label="标题" prop="title">
                <el-input
                  v-model="queryParams.title"
                  placeholder="请输入标题"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="发布人id " prop="cfShopId">
                <el-input
                  v-model="queryParams.cfShopId"
                  placeholder="请输入发布id "
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <div>
                <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
              </div>

            </el-form>

          </div>


          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          <el-table v-loading="loading" :data="activityList">
            <el-table-column label="标题" align="center" prop="title" />
            <el-table-column label="发布内容" align="center" prop="content" />
            <el-table-column label="发布人 " align="center" prop="principalName" />
            <el-table-column label="审核状态" align="center" prop="type">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.cf_shop_audit" :value="scope.row.type"/>
              </template>
            </el-table-column>
            <el-table-column label="发布时间" align="center" prop="createTime" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="small"
                  type="primary"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['active:activity:edit']"
                >审核</el-button>
                <el-button
                  size="small"
                  type="primary"
                  @click="xq(scope.row)"
                >详情</el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
        <div v-if="ifShow==false">
          <div style="display: flex; justify-content: space-between; padding: 10px;">
            <div>信息详情</div>
            <div><el-button type="primary" @click="back">返回</el-button></div>
          </div>
          <div style="border-top: 2px solid #E0E0E0; width: 100%;">
            <div class="activity">
              <div class="hang">
                <div class="left">标题</div>
                <div class="right">{{ form.title }}</div>
              </div>
              <div class="hang">
                <div class="left">发布人</div>
                <div class="right">{{ form.principalName }}</div>
              </div>
              <div class="hang">
                <div class="left">发布时间</div>
                <div class="right">{{ form.createTime }}</div>
              </div>
              <div class="hang">
                <div class="left">内容</div>
                <div class="right">{{ form.content }}</div>
              </div>
              <div class="hang">
                <div class="left2">图片</div>
                <div class="right2">{{ form.title }}</div>
              </div>
            </div>
          </div>
        </div>
      </el-tab-pane>



      <el-tab-pane label="已审核" name="state12">
        <div class="ouner" v-if="ifShow">
          <div class="head">
            <div>发布列表</div>
            <div>
              <el-button type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['active:activity:add']"
                >新增</el-button>
            </div>
          </div>

          <div class="search">
             <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
              <el-form-item label="标题" prop="title">
                <el-input
                  v-model="queryParams.title"
                  placeholder="请输入标题"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="发布人 " prop="principalName">
                <el-input
                  v-model="queryParams.cfShopId"
                  placeholder="请输入发布 "
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <div>
                <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
              </div>

            </el-form>

          </div>


          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          <el-table v-loading="loading" :data="activityList" >
            <el-table-column label="标题" align="center" prop="title" />
            <el-table-column label="发布内容" align="center" prop="content" />
            <el-table-column label="发布人 " align="center" prop="principalName" />
            <el-table-column label="审核状态" align="center" prop="type">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.cf_shop_audit" :value="scope.row.type"/>
              </template>
            </el-table-column>
            <el-table-column label="发布时间" align="center" prop="createTime" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="small"
                  type="primary"
                  @click="xq(scope.row)"
                >详情</el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
        <div v-if="ifShow==false">
          <div style="display: flex; justify-content: space-between; padding: 10px;">
            <div>信息详情</div>
            <div><el-button type="primary" @click="back">返回</el-button></div>
          </div>
          <div style="border-top: 2px solid #E0E0E0; width: 100%;">
            <div class="activity">
              <div class="hang">
                <div class="left">标题</div>
                <div class="right">{{ form.title }}</div>
              </div>
              <div class="hang">
                <div class="left">发布人</div>
                <div class="right">{{ form.principalName }}</div>
              </div>
              <div class="hang">
                <div class="left">发布时间</div>
                <div class="right">{{ form.createTime }}</div>
              </div>
              <div class="hang">
                <div class="left">内容</div>
                <div class="right">{{ form.content }}</div>
              </div>
              <div class="hang">
                <div class="left2">图片</div>
                <div class="right2">{{ form.title }}</div>
              </div>
            </div>
          </div>

        </div>
      </el-tab-pane>
    </el-tabs>

    <!-- 添加或修改信息发布对话框
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="发布 " prop="cfShopId">
          <el-input v-model="form.cfShopId" placeholder="请输入发布 " />
        </el-form-item>
        <el-form-item label="发布内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
     <!-- 审核商户对话框 -->
     <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="审核状态">
              <el-radio-group v-model="form.type">
                <el-radio
                  v-for="dict in filteredAuditOptions"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
        <el-form-item label="审核说明" prop="activeExplain">
          <textarea v-model="form.activityExplain" placeholder="请输入审核说明" cols="30" rows="10"></textarea>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listActivity, getActivity, delActivity, addActivity, updateActivity } from "@/api/active/activity";

export default {
  name: "Activity",
  dicts: ['cf_shop_audit','cf_active_state'],
  data() {
    return {
      activeName:'state1',
      tabPosition:"left",
      ifShow:true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 信息发布表格数据
      activityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        cfShopId: null,
        type: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    nowDate() {
      var getTime = new Date().getTime(); //获取到当前时间戳
      var time = new Date(getTime); //创建一个日期对象
      var year = time.getFullYear(); // 年
      var month = (time.getMonth() + 1).toString().padStart(2, '0'); // 月
      var date = time.getDate().toString().padStart(2, '0'); // 日
      var hour = time.getHours().toString().padStart(2, '0'); // 时
      var minute = time.getMinutes().toString().padStart(2, '0'); // 分
      var second = time.getSeconds().toString().padStart(2, '0'); // 秒
      console.log(year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second)
      return (
        year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second
      )
    },

    /** 查询信息发布列表 */
    getList() {
      this.loading = true;
      listActivity(this.queryParams).then(response => {
        this.activityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        activityId: null,
        title: null,
        cfShopId: null,
        content: null,
        createTime: null,
        type: null,
        auditTime: null,
        activityExplain: null,
        state: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map(item => item.activityId)
    //   this.single = selection.length!==1
    //   this.multiple = !selection.length
    // },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加信息发布";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.activityId || this.ids
      getActivity(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改信息发布";
      });
    },
    /** 修改上下架状态*/
    handleUpdateState(row){
      this.reset();
      const id = row.activityId || this.ids
      getActivity(id).then(response => {
        this.form = response.data;
        this.form.state=this.form.state==1?0:1;
        updateActivity(this.form).then(response => {
          this.$modal.msgSuccess("操作成功");
          this.getList();
        });
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.activityId != null) {
            this.form.auditTime=this.nowDate()
            updateActivity(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addActivity(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除信息发布编号为"' + ids + '"的数据项？').then(function() {
        return delActivity(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('active/activity/export', {
        ...this.queryParams
      }, `activity_${new Date().getTime()}.xlsx`)
    },
    tabClick(tab, event){
      if(tab.name == 'state0'){
        this.queryParams.type=0;
      }else if(tab.name == 'state1'){
        this.queryParams.type=1;
      }else{
        this.queryParams.type=3;
      }
			console.log(this.queryParams)
      this.getList()
    },
    xq(row){
      this.ifShow=false
      this.reset();
      const id = row.activityId || this.ids
      getActivity(id).then(response => {
        this.form = response.data;
        this.title = "修改信息发布";
      });
    },
    back(){
      this.ifShow=true
    }
  },
  computed: {
    filteredAuditOptions() {
      return this.dict.type.cf_shop_audit.filter(dict => dict.value > 0);
    }
  }
};
</script>
<style>
.ouner{
  border: 2px solid #EAEAEA;
  padding: 20px;
  margin: 10px;
}
.search{
  background-color: #F5F5F5;
  padding: 10px 20px;
  margin-bottom: 20px;
}
.head{
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}
.activity{
  border-left: 2px solid #EAEAEA;
  border-right: 2px solid #EAEAEA;
  border-top: 2px solid #EAEAEA;
  margin-top: 20px;
  width: 90%;
}
.hang{
  display: flex;
  border-bottom: 2px solid #ECECEC;
  font-size: 15px;
}
.left{
  border-right: 2px solid #EAEAEA;
  background-color: #EFEFEF;
  text-align: center;
  line-height: 50px;
  width: 300px;
  height: 50px;
}
.right{
  width: 100%;
  height: 50px;
  text-align: center;
  line-height: 50px;
}
.left2{
  border-right: 2px solid #EAEAEA;
  background-color: #EFEFEF;
  text-align: center;
  line-height: 150px;
  width: 300px;
  height: 150px;
}
.right2{
  width: 100%;
  height: 150px;
  text-align: center;
  line-height: 150px;
}
</style>
